<template>
  <!-- 菜单 -->
  <div class="header">
    <div class="header-menu" @click="navToggle">
      <img class="header-menu-icon" src="@/assets/image/header/ic_menu.png" />
    </div>
    <div class="header-logo">
      <img class="header-logo-icon" src="@/assets/image/header/logo.png" />
    </div>
    <router-link class="header-search" to="/search">
      <img
        class="header-search-icon"
        src="@/assets/image/header/ic_search.png"
      />
    </router-link>

    <div class="nav" v-if="navShow">
      <div class="nav-list">
        <div class="nav-item nav-item_active">
          <div class="nav-item-title">葵花药业股票代码（002737）</div>
        </div>
        <div
          class="nav-item"
          v-for="(navItem, navIndex) in nav"
          :key="navItem.id"
        >
          <div class="nav-item-content">
            <a
              class="nav-item-title"
              v-if="navItem.linkurl"
              :href="navItem.linkurl"
              >{{ navItem.name }}</a
            >
            <router-link
              v-else
              class="nav-item-title"
              :to="{ name: navItem.rote }"
            >
              {{ navItem.name }}
            </router-link>
            <div class="nav-item-extend" @click="activeChange(navIndex)">
              <img
                class="nav-item-extend-icon"
                src="@/assets/image/header/right.png"
                v-show="navIndex == active"
              />
              <img
                class="nav-item-extend-icon"
                v-show="navIndex != active"
                src="@/assets/image/header/bottom-arrow.png"
              />
            </div>
          </div>
          <div
            class="menu-list"
            v-show="navIndex == active"
            v-if="navItem.children?.length"
          >
            <template v-for="menuItem in navItem.children" :key="menuItem.id">
              <a
                class="menu-item"
                v-if="menuItem.linkurl"
                :href="menuItem.linkurl"
                >{{ menuItem.name }}</a
              >
              <router-link
                v-else
                class="menu-item"
                :to="{ name: menuItem.rote }"
              >
                {{ menuItem.name }}
              </router-link>
            </template>
          </div>
        </div>
        <div class="nav-item nav-item_active" @click="navClose()">
          <div class="nav-item-content">
            <div class="nav-item-title">网站地图</div>
            <div class="nav-item-extend" @click="activeChange(navIndex)">
              <img
                class="nav-item-extend-icon"
                src="@/assets/image/header/m-close.png"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState("common", ["nav"]),
  },
  data() {
    return {
      navShow: false,
      active: -1,
    };
  },
  watch: {
    $route() {
      this.navShow = false;
    },
  },
  mounted() {},
  methods: {
    navToggle() {
      this.navShow = !this.navShow;
    },
    navOpen() {
      this.navShow = true;
    },
    navClose() {
      this.navShow = false;
    },
    activeChange(index) {
      this.active = this.active == index ? -1 : index;
    },
  },
};
</script>

<style scoped lang="scss">
// 导航
.header {
  position: sticky;
  top: 0;
  height: fn.rpx(90);
  display: flex;
  background-color: #fff;
  align-items: center;
  justify-content: space-between;
  padding: 0 fn.rpx(25);
  // position: relative;
  z-index: 1001;
  border-bottom: fn.rpx(2) solid #eee;

  &-menu {
    width: fn.rpx(40);
    height: fn.rpx(40);

    &-icon {
      width: 100%;
      height: 100%;
      @include fn.icon;
    }
  }

  &-logo {
    width: fn.rpx(159);
    height: fn.rpx(49);

    &-icon {
      width: 100%;
      height: 100%;
      @include fn.icon;
    }
  }

  &-search {
    width: fn.rpx(56);
    height: fn.rpx(56);

    &-icon {
      width: 100%;
      height: 100%;
      @include fn.icon;
    }
  }
}

.nav {
  position: absolute;
  top: calc(100% + fn.rpx(2));
  left: 0;
  width: 100%;
  background-color: #fff;
  z-index: 1001;

  &-list {
    overflow-y: scroll;
    scroll-behavior: smooth;
    max-height: 80vh;
  }

  &-item {
    border-bottom: fn.rpx(2) solid #eee;
    font-size: fn.rpx(28);

    &-content {
      display: flex;
      align-items: center;
    }

    &_active &-title {
      color: color.$base;
    }

    &-title {
      flex: 1;
      height: fn.rpx(100);
      line-height: fn.rpx(100);
      color: #333;
      display: block;
      padding: 0 20px;
    }

    &-extend {
      width: fn.rpx(100);
      height: fn.rpx(100);
      display: flex;
      align-items: center;
      justify-content: center;

      &-icon {
        width: fn.rpx(24);
        height: fn.rpx(24);
        display: block;
      }
    }
  }
}

.menu {
  &-list {
    display: flex;
    flex-wrap: wrap;
    // margin: 0 fn.rpx(-14);
    padding: 0 fn.rpx(28);
    padding-bottom: fn.rpx(28);
  }

  &-item {
    overflow: hidden;
    box-sizing: border-box;
    font-size: fn.rpx(28);
    width: calc(100% / 3 - fn.rpx(28));
    height: fn.rpx(56);
    display: flex;
    justify-content: center;
    align-items: center;
    margin: fn.rpx(14);
    border: fn.rpx(2) solid #999;
    color: #999;
  }
}
</style>

